package com.joyoung.sweep.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

import com.joyoung.sweep.R;

/**
* author: ThinkPad
* date: 2021/4/22
* desc: 协议
*/
public class ProtocolDialog {
    private static ProtocolDialog instance;
    private Context mContext;
    public Dialog dialog;


    private ProtocolDialog(Context context) {
        this.mContext = context;
    }

    public static ProtocolDialog getInstance(Context context) {
        return new ProtocolDialog(context);
    }

    public void showDialog(DialogCallback callback) {
        View view = View.inflate(mContext, R.layout.dialog_tip_layout, null);
        AppCompatDialog dialog = new AppCompatDialog(mContext, R.style.Dialog);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        TextView content = view.findViewById(R.id.tv_desc);
        String string1 = mContext.getString(R.string.user);
        String string2 = mContext.getString(R.string.privacy);
        SpannableStringBuilder spannable1 = new SpannableStringBuilder(string1);
        SpannableStringBuilder spannable2 = new SpannableStringBuilder(string2);
        spannable1.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                callback.protocol();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
        }, 0, spannable1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannable2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                callback.privacy();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
        }, 0, spannable2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        content.setText(mContext.getString(R.string.desc));
        content.append(spannable1);
        content.append("和");
        content.append(spannable2);
        content.setMovementMethod(LinkMovementMethod.getInstance());
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.cancel();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.sure();
                dialog.dismiss();
            }
        });
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
    }

    public interface DialogCallback {
        /**
         * 同意
         */
        void sure();

        /**
         * 取消
         */
        void cancel();

        /**
         * 协议
         */
        void protocol();

        /**
         * 隐私
         */
        void privacy();
    }
}
